package io.renren.modules.humanResources.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import io.renren.common.page.PageData;
import io.renren.common.service.impl.CrudServiceImpl;
import io.renren.common.utils.ConvertUtils;
import io.renren.modules.humanResources.dao.RewardPenaltyDao;
import io.renren.modules.humanResources.dto.RetirementBenefitsDTO;
import io.renren.modules.humanResources.dto.RewardPenaltyDTO;
import io.renren.modules.humanResources.entity.RetirementBenefitsEntity;
import io.renren.modules.humanResources.entity.RewardPenaltyEntity;
import io.renren.modules.humanResources.service.RewardPenaltyService;
import io.renren.modules.security.user.SecurityUser;
import io.renren.modules.security.user.UserDetail;
import io.renren.modules.sys.dto.SysUserDTO;
import io.renren.modules.sys.service.SysUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 奖惩信息表
 *
 * @author Mark sunlightcs@gmail.com
 * @since 1.0.0 2024-03-27
 */
@Service
public class RewardPenaltyServiceImpl extends CrudServiceImpl<RewardPenaltyDao, RewardPenaltyEntity, RewardPenaltyDTO> implements RewardPenaltyService {

    @Autowired
    private SysUserService sysUserService;

    @Override
    public QueryWrapper<RewardPenaltyEntity> getWrapper(Map<String, Object> params){
        String id = (String)params.get("id");
        String realName = (String)params.get("realName");
        String rewardPenaltyType = (String)params.get("rewardPenaltyType");

        QueryWrapper<RewardPenaltyEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(id), "id", id);
        wrapper.eq(StringUtils.isNotBlank(rewardPenaltyType), "reward_penalty_type", rewardPenaltyType);

        if (StringUtils.isNotBlank(realName)) {
            List<SysUserDTO> userDtoList = sysUserService.getByRealName(realName);
            if (CollectionUtils.isNotEmpty(userDtoList)) {
                List<Long> collect = userDtoList.stream().map(SysUserDTO::getId).collect(Collectors.toList());
                wrapper.in("user_id", collect);
            } else {
                return null;
            }
        }

        return wrapper;
    }


    @Override
    public PageData<RewardPenaltyDTO> rewardpenaltyPage(Map<String, Object> params) {
        if (getWrapper(params) == null) {
            return new PageData<RewardPenaltyDTO>(null, 0);
        }
        IPage<RewardPenaltyEntity> page = baseDao.selectPage(
                getPage(params, null, false),
                getWrapper(params)
        );
        PageData<RewardPenaltyDTO> pageData = getPageData(page, RewardPenaltyDTO.class);
        pageData.getList().forEach(e -> {
            SysUserDTO sysUserDto = sysUserService.get(e.getUserId());
            e.setRealName(sysUserDto == null ? "" : sysUserDto.getRealName());
        });
        return pageData;
    }

    @Override
    public List<RewardPenaltyDTO> rewardpenaltyList() {
        // 获取用户信息
        UserDetail user = SecurityUser.getUser();
        // 根据用户查询
        QueryWrapper<RewardPenaltyEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id", user.getId());
        wrapper.orderByDesc("create_date");
        List<RewardPenaltyEntity> entityList = this.baseDao.selectList(wrapper);
        List<RewardPenaltyDTO> dtoList = ConvertUtils.sourceToTarget(entityList, RewardPenaltyDTO.class);
        dtoList.forEach(e -> {
            SysUserDTO sysUserDto = sysUserService.get(e.getUserId());
            e.setRealName(sysUserDto == null ? "" : sysUserDto.getRealName());
        });
        return dtoList;
    }
}